package com.gateway.plugin.api;

import com.gateway.common.enums.RpcTypeEnum;
import com.gateway.plugin.api.context.GatewayContext;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.core.Ordered;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilter;
import reactor.core.publisher.Mono;

import java.util.Objects;

/**
 * @program: ahcloud-gateway
 * @description:
 * @author: YuKai Fan
 * @create: 2024/5/18 10:23
 **/
public interface GatewayPlugin {

    /**
     * Process the Web request and (optionally) delegate to the next
     * {@code WebFilter} through the given {@link GatewayPluginChain}.
     *
     * @param exchange the current server exchange
     * @param chain    provides a way to delegate to the next filter
     * @return {@code Mono<Void>} to indicate when request processing is complete
     */
    Mono<Void> execute(ServerWebExchange exchange, GatewayPluginChain chain);

    /**
     * return plugin order .
     * This attribute To determine the plugin execution order in the same type plugin.
     *
     * @return int order
     */
    int getOrder();

    /**
     * 获取插件名称。
     * 这是插件名，你必须提供正确的名称。
     * 如果你impl AbstractGatewayPlugin 这个属性不使用
     *
     * @return plugin name.
     */
    default String named() {
        return "";
    }

    /**
     *  plugin被执行。
     *  如果返回true，这个插件不能执行。
     *
     * @param exchange the current server exchange
     * @return default false.
     */
    default boolean skip(ServerWebExchange exchange) {
        return false;
    }

    /**
     * 除了一些RPC类型外，插件执行跳过。
     * 如果返回true，这个插件不能执行
     *
     * <p>the same for:
     * <pre>
     * <code>Objects.equals(rpcType, typeA.getName())
     * || Objects.equals(rpcType, typeB.getName())
     * || Objects.equals(rpcType, type...getName())
     * </code>
     * </pre>
     *
     * @param exchange the current server exchange
     * @param rpcTypes the skip rpc type list
     * @return current rpcType == someone rpcType
     */
    default boolean skip(ServerWebExchange exchange, RpcTypeEnum... rpcTypes) {
        if (ArrayUtils.isEmpty(rpcTypes)) {
            return false;
        }
        GatewayContext gatewayContext = exchange.getAttribute(GatewayContext.CACHE_GATEWAY_CONTEXT);
        assert gatewayContext != null;
        RpcTypeEnum rpcType = gatewayContext.getRpcType();
        for (final RpcTypeEnum type : rpcTypes) {
            if (Objects.equals(rpcType, type)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 除了一些RPC类型外，插件执行跳过。
     * 如果返回true，这个插件不能执行
     *
     * <p>the same for:
     * <pre>
     * <code>!Objects.equals(rpcType, typeA.getName())
     * &amp;&amp; !Objects.equals(rpcType, typeB.getName())
     * &amp;&amp; !Objects.equals(rpcType, type...getName())
     * </code>
     * </pre>
     *
     * @param exchange the current server exchange
     * @param exceptRpcTypes the except rpc type list
     * @return current rpcType != someone exceptRpcType
     */
    default boolean skipExcept(ServerWebExchange exchange, RpcTypeEnum... exceptRpcTypes) {
        return !skip(exchange, exceptRpcTypes);
    }

    /**
     * 跳过非http调用。
     * 如果返回true，这个插件不能执行。
     *
     * @param exchange the current server exchange
     * @return http/spring cloud return true, others false.
     */
    default boolean skipExceptHttpLike(ServerWebExchange exchange) {
        return !skip(exchange, RpcTypeEnum.HTTP, RpcTypeEnum.SPRING_CLOUD);
    }


}
